﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure;
using B.S.BaseData.Write.API.Application.Command;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.BaseData.Write.API.Application.CommandHnadler
{
    /// <summary>
    /// 商品修改命令处理
    /// </summary>
    public class GoodsUpdateCommandHandler : IRequestHandler<GoodsUpdateCommand,ApiResult<object>>
    {
        /// <summary>
        /// 商品信息仓储
        /// </summary>
        private readonly IBaseRepository<GoodsInfo> goodsRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="goodsRepository">商品信息仓储</param>
        /// <param name="mapper">映射</param>
        public GoodsUpdateCommandHandler(IBaseRepository<GoodsInfo> goodsRepository,IMapper mapper)
        {
            this.goodsRepository = goodsRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<object>> Handle(GoodsUpdateCommand request, CancellationToken cancellationToken)
        {
            //ApiResult<object> result = new ApiResult<object>();
            //result.Code = ApiEnums.修改成功;
            //result.Msg = ApiEnums.修改成功.ToString();

            //var use = await goodsRepository.GetAll().FirstOrDefaultAsync(x => x.GoodsId == request.GoodsId);
            //use.Gid = request.Gid;
            //use.Expiration = request.Expiration;
            //use.Production = request.Production;
            //use.ProductionNumber = request.ProductionNumber;
            //use.ProductionHome = request.ProductionHome;
            //use.UpdateName = "张三";
            //int i = goodsRepository.Update(use);
            //result.Data = i;
            //return await Task.FromResult(result);
            ApiResult<object> result = new ApiResult<object>();

            // 从仓储中获取商品信息
            var goods = await goodsRepository.GetAll().FirstOrDefaultAsync(x => x.GoodsId == request.GoodsId);

            // 检查商品是否存在
            //if (goods == null)
            //{
            //    result.Code = ApiEnums.修改失败; 
            //    result.Msg = ApiEnums.修改失败.ToString();
            //    result.Data = null;
            //    return await Task.FromResult(result);
            //}

            // 更新商品信息
            //goods.Gid = request.Gid;
            goods.Expiration = request.Expiration;
            goods.ProductionHome = request.ProductionHome;
            goods.UpdateName = "张三";

            // 调用仓储的更新方法
            int rowsAffected = goodsRepository.Update(goods);

            // 设置返回结果
            result.Code = ApiEnums.修改成功;
            result.Msg = ApiEnums.修改成功.ToString();
            result.Data = rowsAffected;

            return await Task.FromResult(result);
        }
    }
}
